package com.example.projectmanagement.model.study;

import com.example.projectmanagement.model.User;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import java.time.LocalDateTime;

@Entity
@Table(name = "study_answer")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class Answer {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 5000)
    private String content;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "question_id", nullable = false)
    private Question question;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_answer_id")
    private Answer parentAnswer;
    
    @Transient
    private Long parentAnswerId;

    @Column(name = "like_count")
    private Integer likeCount = 0; // 点赞数

    @Column(name = "dislike_count")
    private Integer dislikeCount = 0; // 点踩数

    @Column(name = "is_accepted")
    private Boolean isAccepted = false; // 是否被采纳为最佳答案

    @CreatedDate
    @Column(name = "create_time", updatable = false)
    private LocalDateTime createTime;

    @LastModifiedDate
    @Column(name = "update_time")
    private LocalDateTime updateTime;
    
    // 添加getter和setter方法
    public Long getParentAnswerId() {
        if (this.parentAnswerId != null) {
            return this.parentAnswerId;
        }
        if (this.parentAnswer != null) {
            return this.parentAnswer.getId();
        }
        return null;
    }
    
    public void setParentAnswerId(Long parentAnswerId) {
        this.parentAnswerId = parentAnswerId;
    }
}